{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np          # 导入numpy包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# https://blog.csdn.net/Sakura_Logic/article/details/103889314"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1, 2, 3])     # 输入一维矩阵，英文单词array意思为:阵列，矩阵的意思。所以方法array()，也就顾名思义了。\n",
    "print(a)                    # 输出结果为：[1 2 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "# 构造二维的矩阵。\n",
    "b = np.array([[1, 2, 3],\n",
    "             [4, 5, 6]])\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 1  2  3]\n",
      "  [ 4  5  6]]\n",
      "\n",
      " [[ 7  8  9]\n",
      "  [10 11 12]]]\n"
     ]
    }
   ],
   "source": [
    "# 构造三维矩阵：\n",
    "c = np.array([[[1, 2, 3], [4, 5, 6]],\n",
    "              [[7, 8, 9], [10, 11, 12]]])\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 总结：一维矩阵一层中括号，二维矩阵两层中括号，三维矩阵三层中括号，继续下去一样，输入几维矩阵就是几层中括号。常用的只是一维和二维。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "# 定义一个 3 * 3 的二维矩阵。\n",
    "array = np.array([[1, 2, 3],\n",
    "                  [4, 5, 6],\n",
    "                  [7, 8, 9]])\n",
    "print(array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(array.ndim)           # array.ndim查看矩阵的维度。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 3)\n"
     ]
    }
   ],
   "source": [
    "print(array.shape)          # array.shape查看矩阵的形状，返回几行几列的矩阵。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    }
   ],
   "source": [
    "print(array.size)           # array.size查看元素个数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int64\n"
     ]
    }
   ],
   "source": [
    "print(array.dtype)          # array.dtype查看矩阵中元素类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int32\n",
      "[1 2 3]\n",
      "float64\n",
      "[1. 2. 3.]\n",
      "[[0. 0. 0.]\n",
      " [0. 0. 0.]]\n",
      "[[1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]]\n",
      "[[0. 0. 0.]\n",
      " [0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "# 输入一维矩阵，参数dtype=np.int32定义元素值的类型。\n",
    "a = np.array([1, 2, 3], dtype=np.int32)\n",
    "print(a.dtype)              # 返回：int32\n",
    "print(a)                    # 返回：[1 2 3]\n",
    "\n",
    "b = np.array([1, 2, 3], dtype=np.float)\n",
    "print(b.dtype)              # 返回：float64\n",
    "print(b)                    # 返回：[1. 2. 3.]\n",
    "# 矩阵b与矩阵a相比较，返回的元素值都加了点，为浮点型数值。\n",
    "\n",
    "# zeros()返回一个全为零的矩阵，参数(2, 3)表示矩阵规格为2行3列。\n",
    "zero = np.zeros((2, 3))\n",
    "print(zero)\n",
    "# 返回结果：\n",
    "# [[0. 0. 0.]\n",
    "#  [0. 0. 0.]]\n",
    "\n",
    "# ones()返回一个全为1的矩阵，参数(2, 4)同样表示矩阵规格，2行4列\n",
    "one = np.ones((2, 4))\n",
    "print(one)\n",
    "# 返回结果如下：\n",
    "# [[1. 1. 1. 1.]\n",
    "#  [1. 1. 1. 1.]]\n",
    "\n",
    "# empty()返回一个空矩阵，参数(2, 3)同样表示矩阵规格。\n",
    "empty = np.empty((2, 3))\n",
    "print(empty)\n",
    "# 返回结果：\n",
    "# [[0. 0. 0.]\n",
    "#  [0. 0. 0.]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[4 5 6 7 8 9]\n",
      "[ 1  4  7 10 13]\n",
      "[[0 1 2 3]\n",
      " [4 5 6 7]]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(10)           # 返回一维数组，不包括10，从0~9。\n",
    "print(a)                    # 返回结果：[0 1 2 3 4 5 6 7 8 9]\n",
    "\n",
    "b = np.arange(4, 10)        # 返回一维数组，不包括10，从4~9。\n",
    "print(b)                    # 返回结果：[4 5 6 7 8 9]\n",
    "\n",
    "c = np.arange(1, 15, 3)     # 返回一维数组，不包括15，从1~15中，依次加3取数。\n",
    "print(c)                    # 返回结果：[ 1  4  7 10 13]\n",
    "\n",
    "d = np.arange(8).reshape(2, 4)   # 单词reshape为重新塑造的意思，即将原来0~7的一维数组，从新塑造成2行4列的数组。\n",
    "print(d)\n",
    "# 返回结果如下：\n",
    "# [[0 1 2 3]\n",
    "#  [4 5 6 7]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 3 5]\n",
      " [6 8 9]]\n",
      "[[0 1 1]\n",
      " [2 2 3]]\n",
      "[[ 1  2  6]\n",
      " [ 8 15 18]]\n",
      "[[  1   2   9]\n",
      " [ 16 125 216]]\n",
      "[[1.         2.         1.5       ]\n",
      " [2.         1.66666667 2.        ]]\n",
      "[[1 2 1]\n",
      " [2 1 2]]\n",
      "[[0 0 1]\n",
      " [0 2 0]]\n",
      "[[3 4 5]\n",
      " [6 7 8]]\n",
      "[[1 0 1]\n",
      " [0 1 0]]\n",
      "[[False False False]\n",
      " [ True  True  True]]\n"
     ]
    }
   ],
   "source": [
    "# 对位运算：对应位置的元素进行运算\n",
    "# 手动输入两个矩阵，进行加、减、乘、除、幂运算、取整、取余以及矩阵乘法与转置的运算操作。\n",
    "a = np.array([[1, 2, 3],\n",
    "             [4, 5, 6]])\n",
    "\n",
    "b = np.array([[1, 1, 2],\n",
    "             [2, 3, 3]])\n",
    "# 其中加、减、乘、除、幂运算、取整、取余，为一一对位运算，要求两个矩阵的行数与列数相同。\n",
    "\n",
    "print(a + b)\n",
    "# 对应相加后的结果为：\n",
    "# [[2 3 5]\n",
    "#  [6 8 9]]\n",
    "\n",
    "print(a - b)\n",
    "# [[0 1 1]\n",
    "#  [2 2 3]]\n",
    "\n",
    "print(a * b)\n",
    "# 这个注意一下，与线性代数中矩阵相乘不同，此处为一一对位相乘。矩阵运算在下面。\n",
    "# [[ 1  2  6]\n",
    "#  [ 8 15 18]]\n",
    "\n",
    "print(a ** b)\n",
    "# 同样对应取幂，1的1次方，2的1次方，3的2次方...\n",
    "# [[  1   2   9]\n",
    "#  [ 16 125 216]]\n",
    "\n",
    "print(a / b)\n",
    "# [[1.         2.         1.5       ]\n",
    "#  [2.         1.66666667 2.        ]]\n",
    "\n",
    "print(a // b)\n",
    "# [[1 2 1]\n",
    "#  [2 1 2]]\n",
    "\n",
    "print(a % b)\n",
    "# [[0 0 1]\n",
    "#  [0 2 0]]\n",
    "\n",
    "# 下面是矩阵加、减、乘、除、取幂、取整、取余一个数，规则一样\n",
    "\n",
    "print(a + 2)\n",
    "# 所有元素都加上2。\n",
    "# [[3 4 5]\n",
    "#  [6 7 8]]\n",
    "# ......中间略去......\n",
    "\n",
    "print(a % 2)\n",
    "# 同样每个数都对2取余。\n",
    "# [[1 0 1]\n",
    "#  [0 1 0]]\n",
    "\n",
    "c = a > 3\n",
    "print(c)\n",
    "# 所有元素与3比较，比3大返回True，否则返回False。\n",
    "# [[False False False]\n",
    "#  [ True  True  True]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 6.  6.  6.  6.]\n",
      " [15. 15. 15. 15.]]\n",
      "[[ 6.  6.  6.  6.]\n",
      " [15. 15. 15. 15.]]\n",
      "[[1 4]\n",
      " [2 5]\n",
      " [3 6]]\n",
      "[[1 4]\n",
      " [2 5]\n",
      " [3 6]]\n",
      "[[-0.94444444  0.44444444]\n",
      " [-0.11111111  0.11111111]\n",
      " [ 0.72222222 -0.22222222]]\n"
     ]
    }
   ],
   "source": [
    "# 常用的矩阵运算，需要有一点线性代数或矩阵基础。\n",
    "d = np.ones((3, 4))  # 生成一个3行4列，全为1的矩阵。\n",
    "\n",
    "# 矩阵乘法两种方式\n",
    "\n",
    "# 第一种：\n",
    "print(np.dot(a, d))\n",
    "# 第二种：\n",
    "print(a.dot(d))\n",
    "# 运算规则，第一个矩阵的列必须与第二个矩阵的行相等才能运算\n",
    "# 2行3列的矩阵只能与3行4列的矩阵相乘\n",
    "# [[ 6.  6.  6.  6.]\n",
    "#  [15. 15. 15. 15.]]\n",
    "\n",
    "# 矩阵转置，本质为行列互换。两种方式。\n",
    "# 第一种：\n",
    "print(a.T)\n",
    "# 第二种：\n",
    "print(np.transpose(a))\n",
    "# [[1 4]\n",
    "#  [2 5]\n",
    "#  [3 6]]\n",
    "\n",
    "# 逆矩阵\n",
    "print(np.linalg.pinv(a)) \n",
    "# [[-0.94444444  0.44444444]\n",
    "#  [-0.11111111  0.11111111]\n",
    "#  [ 0.72222222 -0.22222222]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
